﻿// 大作业初体验.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

#include "pch.h"
#include <graphics.h>
#include<stdlib.h>
#include <conio.h>
//#pragma comment(lib,"Winmm.lib")
//put all below into a function ,and set up a choose 
//function to choose what funtion to execute???
IMAGE startbk;
IMAGE background;
IMAGE man1[2][5], man2[2][5];
IMAGE grass;
IMAGE tree;//地图元素,0是树，1是苹果
IMAGE apple;
IMAGE chooseblock;
IMAGE choosestick;
int tree_x, tree_y;
int startstick_x, startstick_y;//存档刺的位置
int startblock_x, startblock_y;//存档块的位置
int i, j;//man move processi==right,j==left
int direction;
int input[2];//to know if input exist
int man_x, man_y;
int man_tx, man_ty;
int grass_zy;
int g;//加速度
int man_vy;
int step_x, step_y;
int count;//按键计数器,用于控制按跳跃的次数，还不完善
int apple_x, apple_y;
int passcount;//经过次数判定
int Interface;
int manlen;
int manhigh;
int applelen;
int applehigh;
int gamestatus[3];//游戏状态,
//void startshow()
//{
//	loadimage(&choosebk, _T("D:\\1大作业\\timg.jpg"));//这里要改
//	putimage(0, 0, &choosebk);
//}
//void writerecordfile()//存档
//{
//	FILE*fp;
//	fp = fopen(".\\gameRecord.dat", "w");
//	fprintf(fp, "%d%d", i, j);
//	fprintf(fp, "%d", direction);
//	fprintf(fp, "%d", input[0]);
//	fprintf(fp, "%d", input[1]);
//	fprintf(fp, "%d%d", man_x, man_y);
//	fprintf(fp, "%d%d", man_tx, man_ty);
//	fprintf(fp, "%d", grass_zy);
//	fprintf(fp, "%d", g);
//	fprintf(fp, "%d",man_vy);
//	fprintf(fp, "%d%d", step_x, step_y);
//	fprintf(fp, "%d", count);
//	fprintf(fp, "%d%d", tree_x, tree_y);
//	fprintf(fp, "%d%d", apple_x, apple_y);
//	fprintf(fp, "%d", passcount);
//	fprintf(fp, "%d", Interface);
//	fprintf(fp, "%d", manlen);
//	fprintf(fp, "%d", manhigh);
//	fprintf(fp, "%d", applelen);
//	fprintf(fp, "%d", applehigh);
//	fprintf(fp, "%d", gamestatus[0]);
//	fprintf(fp, "%d", gamestatus[1]);
//	fprintf(fp, "%d", gamestatus[2]);
//}
//void writerecordfile()//读档
//{
//	FILE*fp;
//	fp = fopen(".\\gameRecord.dat", "r");
//	fscanf(fp, "%d%d", i, j);
//	fscanf(fp, "%d", direction);
//	fscanf(fp, "%d", input[0]);
//	fscanf(fp, "%d", input[1]);
//	fscanf(fp, "%d%d", man_x, man_y);
//	fscanf(fp, "%d%d", man_tx, man_ty);
//	fscanf(fp, "%d", grass_zy);
//	fscanf(fp, "%d", g);
//	fscanf(fp, "%d", man_vy);
//	fscanf(fp, "%d%d", step_x, step_y);
//	fscanf(fp, "%d", count);
//	fscanf(fp, "%d%d", tree_x, tree_y);
//	fscanf(fp, "%d%d", apple_x, apple_y);
//	fscanf(fp, "%d", passcount);
//	fscanf(fp, "%d", Interface);
//	fscanf(fp, "%d", manlen);
//	fscanf(fp, "%d", manhigh);
//	fscanf(fp, "%d", applelen);
//	fscanf(fp, "%d", applehigh);
//	fscanf(fp, "%d", gamestatus[0]);
//	fscanf(fp, "%d", gamestatus[1]);
//	fscanf(fp, "%d", gamestatus[2]);
//}
void startup()
{
	direction = 0;//0==right,1==left,-1=stable
	man_x = 100;
	man_y = 450;
	g = 20;
	man_vy = 0;
	man_tx = 0;
	man_ty = 0;
	grass_zy = 500;
	step_y = 40;//
	step_x = 20;
	count = 0;
	passcount = 0;
	Interface = 1;
	input[0] = 0;
	input[1] = 0;
	//initgraph(1100, 560);
	loadimage(&background, _T("D:\\1大作业\\timg.jpg"));
	loadimage(&man1[0][0], _T("D:\\1大作业\\MAN1.png"));
	loadimage(&man1[0][1], _T("D:\\1大作业\\man2.png"));//50*50
	loadimage(&man1[0][2], _T("D:\\1大作业\\man3.png"));
	loadimage(&man1[0][3], _T("D:\\1大作业\\man4.png"));//50*50
	loadimage(&man2[0][0], _T("D:\\1大作业\\man1_b.png"));
	loadimage(&man2[0][1], _T("D:\\1大作业\\man2_b.png"));//50*50
	loadimage(&man2[0][2], _T("D:\\1大作业\\man3_b.png"));
	loadimage(&man2[0][3], _T("D:\\1大作业\\man4_b.png"));//50*50
	//loadimage(&man1[0][4], _T("D:\\1大作业\\man5.png"));
	//loadimage(&man2[0][2], _T("D:\\1大作业\\MAN22.png"));//50*50
	//loadimage(&man1[0][1], _T("D:\\1大作业\\MAN11.png"));
	//loadimage(&man2[0][2], _T("D:\\1大作业\\MAN22.png"));//50*50
	//loadimage(&man1[0][1], _T("D:\\1大作业\\MAN11.png"));
	//loadimage(&man2[0][2], _T("D:\\1大作业\\MAN22.png"));//50*50
	loadimage(&grass, _T("D:\\1大作业\\地_副本.jpg"));//550*60
	//loadimage(&tree, _T(""));
	//loadimage(&apple, _T(""));
	BeginBatchDraw();
	//人物移动图片，这里在注释中给出
	/*loadimage(&man1, _T("D:\\1大作业\\移动.png"));*/
	/*mciSendString(_T("open D:\\1大作业\\background.mp3 alias mymusic"), NULL, 0, NULL);
	mciSendString(_T("play mymusic"), NULL, 0, NULL);*/

}
void variable_change()
{
	if (man_y + 50 < grass_zy)
	{
		man_vy += g;
		man_y += man_vy / 2;
		while (man_y + man_vy / 2 + 50 > grass_zy)
		{
			man_vy -= 10;
		}
	}
	else
	{
		man_vy = 0;
		count = 0;
	}
}
void show()
{
	putimage(0, 0, &background);
	putimage(0, 500, &grass);
	putimage(550, 500, &grass);//若要对草进行改动,比如裂开，则另设变量，否则可以用number
	if (input[1] || input[0])
	{
		if (!direction)//0==right,1==left.i=right,j=left
		{
			putimage(man_x, man_y, &man2[direction][i], NOTSRCERASE);
			putimage(man_x, man_y, &man1[direction][i], SRCINVERT);
			i++;
			if (i > 4)
				i = 0;
		}
		else
		{
			putimage(man_x, man_y, &man2[direction][j], NOTSRCERASE);
			putimage(man_x, man_y, &man1[direction][j], SRCINVERT);
			j++;
			if (j > 4)
				j = 0;
		}
		if (input[1] && !input[0])
		{
			i = 0;
		}
		else if (input[0] && !input[1])
		{
			j = 0;
		}
	}
	//input[0] = 0;
	//input[1] = 0;
	if (!input[0] && !input[1])
	{
		putimage(man_x, man_y, &man2[direction][0], NOTSRCERASE);
		putimage(man_x, man_y, &man1[direction][0], SRCINVERT);
	}
	input[0] = 0;
	input[1] = 0;
	//putimage(_x, , &man1, NOTSRCERASE);//tree
	//putimage(_x, man_y, &man1, NOTSRCERASE);//apple
	Sleep(50);
	FlushBatchDraw();
}
//void trap()//1100*560
//{
//	if (man_x > tree_x)
//		tree_y -= 50;
//	if (man_x > apple_x)
//		passcount++;
//	if(passcount==1&&man_x<apple_x+applelen+50)
//		apple_y -= 30;
//}

void move()
{
	if (GetAsyncKeyState(VK_LEFT) & 0x8000)
	{
		man_x -= step_x;//a
		direction = 1;
		input[0] = 0;
		input[1] = 1;
		/*input[1] = 1;
		if (i >= 4)
			i = 0;
		putimage(man_x, man_y, &man2[direction][i], NOTSRCERASE);
		putimage(man_x, man_y, &man1[direction][i], SRCINVERT);
		i++;
		input[1] = 0;
		FlushBatchDraw();
		Sleep(50);*/
	}
	if ((GetAsyncKeyState(VK_RIGHT) & 0x8000)) // d asVK_LSHIFT  VK_SPACE
	{
		man_x += step_x;
		direction = 0;
		input[0] = 1;
		input[1] = 0;
		/*input[0] = 1;
		if (j >= 4)
			j = 0;
		putimage(man_x, man_y, &man2[direction][j], NOTSRCERASE);
		putimage(man_x, man_y, &man1[direction][j], SRCINVERT);
		j++;
		input[0] = 0;
		FlushBatchDraw();
		Sleep(50);*/
	}
	if ((GetAsyncKeyState(VK_SPACE) & 0x8000) && (count < 4))// 上方向键
	{
		count++;
		man_vy += -step_y;
		man_y += man_vy / 2;
	}


}

//void statetest()//状态检测
//{
//	if((man_x<apple_x + applelen)&&(apple_y - applehigh < man_y)&&passcount==1)
//		Interface = 2;//死亡
//	if (apple_y - applehigh < man_y&&man_x - manlen < apple_x)
//		Interface = 2;
//
//}
void choosestartup()
{
	startstick_x = 100;
	startstick_y = 450;
	startblock_x = 80;
	startblock_y = 80;
}
void gameover()
{
	EndBatchDraw();
	_getch();
	closegraph();
}
void startchoose()//主要函数
{
	
	choosestartup();
	setcolor(WHITE);//测试
	loadimage(&choosestick, _T("D:\\1大作业\\存档刺.jpg"));//放置刺的图片
	loadimage(&choosestick, _T("D:\\1大作业\\存档块.jpg"));//放置块的图片
	putimage(startstick_x, startstick_y, &choosestick);
	putimage(startblock_x, startblock_y, &chooseblock);//注意存档内容
	int choosenum=0;//考虑要不要在开头定义，做全局变量并且保存，这里暂时定义在函数中//初始化,没设全局可能有问题
	if (GetAsyncKeyState(VK_LEFT) & 0x8000)
	{
		choosenum++;
		if (choosenum == 3)
		{
			choosenum = 0;
		}
		
		putimage(startstick_x*(choosenum+1),startstick_y*(choosenum+1), &choosestick);
	}
	
	if (GetAsyncKeyState(VK_RIGHT) & 0x8000)
	{
		choosenum++;
		if (choosenum == -1)
		{
			choosenum = 3;
		}
		
		putimage(startstick_x*(choosenum + 1), startstick_y*(choosenum + 1), &choosestick);

	}
	if (GetAsyncKeyState(VK_SHIFT) & 0x8000)
	{
		if (gamestatus[choosenum] == 0)
		{
			while (1)
			{
				variable_change();
				show();
				move();
				//trap();
			}
		}
		if (gamestatus[choosenum] == 1)
		{
			//writerecordfile();//第一个存档，注意修改内容
				while (1)
				{
					variable_change();
					show();
					move();
					//trap();
				}
		}
		if (gamestatus[choosenum] == 2)
		{
			//writerecordfile();//第二个，注意修改内容
			while (1)
			{
				variable_change();
				show();
				move();
				//trap();
			}
		}
	}
	
}

void main()
{
	
		initgraph(1100, 560);
		BeginBatchDraw();
		loadimage(&background, _T("‪D:\\1大作业\\timg.jpg"));
		putimage(0, 0, &background);
		FlushBatchDraw();
	//if (GetAsyncKeyState(VK_SHIFT) & 0x8000)
	//{
	//	startup();
	//	startchoose();//包含所有函数
	//	gameover();
	//}
	gameover();
			
}//树叶滚动，碰撞检测
//画面输出
//刺的检测
//
